package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Department;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Pair;

public class ViewStaff extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public ViewStaff()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, String dptID)
	{
		String content = "<h1>Προσωπικό</h1>";
		
		content += "<h2>Τμήματα</h2>";
		
		List<List<String>> rootDpts = Department.getRootDpts();
		
		if(rootDpts == null)
			content += "Πρόβλημα με την φόρτωση των τμημάτων.";
		else
		{			
			content += "<ul class=\"perdepList\">";			
			
			List<Pair<List<String>, Integer>> L = new ArrayList<Pair<List<String>, Integer>>();
			
			for(List<String> root : rootDpts)
			{
				L.add(new Pair<List<String>, Integer>(root, 0));
				List<Pair<List<String>, Integer>> children = Department.getAllChildren(root.get(0)); 
				if(children != null) L.addAll(children);		
			}
			
			int depth;
			int currdepth = 0;
			int openuls = 0;
			for(Pair<List<String>, Integer> pair : L)
			{
				depth = pair.getSecond();
				if(depth > currdepth)
				{
					for(int d = 0; d < depth - currdepth; d++)
					{
						content += "<li class=\"nobullet\"><ul class=\"perdepList\">";
						openuls++;
					}
				}
				else if(depth < currdepth)
				{
					for(int d = 0; d < currdepth - depth; d++)
					{
						content += "</ul></li>";
						openuls--;
					}
				}	
				content += "<li><a href=\"ViewStaff?id=" + pair.getFirst().get(0) + "#staff\">" + pair.getFirst().get(1) + "</a></li>";
				currdepth = depth;
			}			
	
			for(int x = 0; x < openuls; x++) content += "</ul></li>";
			
			content += "</ul>";
			
			List<List<String>> users = Department.getUsers(dptID);		
			
			content += "<a name=\"staff\" />";
			content += "<h2>Προσωπικό Τμήματος</h2>";
			
			if(dptID == null)
			{
				content += "Επιλέξτε τμήμα από την λίστα.";				
			}
			else if(users == null)
			{
				content += "Παρουσιάστηκε πρόβλημα με την φόρτωση των χρηστών.";
			}
			else if(users.size() == 0)
			{
				content += "Το τμήμα που επιλέξατε δεν περιέχει χρήστες ακόμα.";			
			}
			else
			{
				for(List<String> user : users)
				{
					String userID = user.get(0);
					String name = user.get(1);
					String pos = user.get(2);
					String photo = user.get(3) == null?"images/profilePictures/placeholder.png":user.get(3);
					String mail = user.get(4);
					
					content += "<a href=\"Profile?id=" + userID + "\"><img class=\"personnelImage\"	src=\"" + photo +"\" alt=\"\" /></a>";
					content += "<table><tr><td colspan=\"2\"><h4 class=\"name\">";
					content += "<a href=\"Profile?id=" + userID + "\">" + name + "</a></h4></td></tr>";
					content += "<tr><td>Θέση:</td><td>" + pos + "</td></tr>"; 
					content += "<tr><td>email:</td><td>" + mail + "</td></tr>"; 
					content += "<tr><td colspan=\"2\">" +
							"<a class=\"genericLink\" href=\"Blog?uid=" + userID + "\">Εμφάνιση Ιστολογίου Χρήστη</a></td></tr>";
					content += "<tr><td colspan=\"2\">" +
							"<a class=\"genericLink\" href=\"SendMessage?id=" + userID + "\">Αποστολή Προσωπικού Μηνύματος</a></td></tr>";
					content += "</table><div class=\"clear\"></div><br/>";				
				}
				
			}
		}
		return HTMLPages.getGeneralPage(content, uid);	
	}
		
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			response.setContentType("text/html; charset=UTF-8");	
			
			request.setCharacterEncoding("utf-8");
			String dptID = request.getParameter("id");
			
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, dptID));
			
			out.close();			
		}
	}
	
}
